package com.qf.tenant.manager.service.impl;

import com.qf.common.bean.R;
import com.qf.common.dto.ProductDto;
import com.qf.common.model.QfProduct;
import com.qf.common.utils.IdWorker;
import com.qf.common.utils.QfUtil;
import com.qf.tenant.manager.feign.ProductFeign;
import com.qf.tenant.manager.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 千锋健哥
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductFeign productFeign;

    @Override
    public R insertProduct(ProductDto dto) {
        //将dto转换成实体pojo
        QfProduct product = new QfProduct();
        dto.convertDtoToDo(product);

        //初始化产品key, product_行政区编码_唯一标识
        IdWorker idWorker = new IdWorker();
        String productKey = "product_" + product.getAreaCode() + "_" + idWorker.nextId();
        product.setProductKey(productKey);

        //产品用户名, 默认产品用户名和productKey保持一致
        product.setUsername(productKey);

        //产品密码, 默认密码是123456, 使用md5加密
        product.setPassword(QfUtil.md5("12345"));

        //创建时间
        product.setCreateTime(new Date());
        //修改时间
        product.setUpdateTime(new Date());
        //设置删除标记
        product.setDeleted(0);

        productFeign.add(product);
        return R.ok(product);
    }

}
